import * as logger from './loglevel';
import moment from 'moment';
/* eslint-disable*/



const timeType={
  BLOCK_TIME:'block_time',
  DOM_READY:'dom_ready',
  LOAD_TIME:'load_time'
}
const logType={
  DEBUG:'debug',
  ERROR:'erroe',
  INFO:'info',
  TRACE:'trace',  // 0
  WARN:'warn',   // 
}

  // logger.info('DEBUG',logger.levels.DEBUG);
  // logger.info('ERROR',logger.levels.ERROR);
  // logger.info('INFO',logger.levels.INFO);
  // logger.info('TRACE',logger.levels.TRACE);
  // logger.info('WARN',logger.levels.WARN);


class Rlog {
  reqid = '';

  userId = '';

  app = null;

  mountTime=0;

  upDataTime=0;

  constructor() {

    logger.setDefaultLevel('trace');
    console.log('constructor-------------------------xxxx');
    window.onerror = function(message, source, lineno, colno,error){
      console.log('error-------------------------xxxx');
      this.allErrorFun(message, source, lineno, colno, error);
    }

    window.addEventListener('error',this.handleListenerError, true);
    this.init();
  }

  init(a, v) {
    console.log('init-------------------------xxxx');
   
  }

  allErrorFun(message, source, lineno, colno, error){
    console.log('发现异常',message);
  }


  handleListenerError(eventErr){
    console.log('发现异常2',eventErr);
    　eventErr.preventDefault() // 阻止浏览器console输出

  }
  // 初始化数据
  // eslint-disable-next-line
  init(a, v) {}

  // 设置默认日志等级
  setloglevel(level){
    logger.setDefaultLevel(level);
  }


  // 设置当前时间戳
  setTimeStamp(){
    this.mountTime=new Date().getTime();
  }

  // 获取时间戳
  getDateString() {
    const str = moment().format('MMMM-Do-YYYY hh:mm:ss');
    return '[' + str + ']';
  }

  //获取页面性能信息(H5)
  getPerformanceTiming(type) {
    var performance = window.performance;
    if (!performance) {
      console.log('您的浏览器不支持performance属性');
      return;
    }
    
    var t = performance.timing;
    var obj = { timing: performance.timing };
    // 重定向耗时
    obj.redirectTime = t.redirectEnd - t.redirectStart;
    // DNS查询耗时
    obj.lookupDomainTime = t.domainLookupEnd - t.domainLookupStart;
    // TCP链接耗时
    obj.connectTime = t.connectEnd - t.connectStart;
    // HTTP请求耗时
    obj.requestTime = t.responseEnd - t.responseStart;
    // 解析dom树耗时
    obj.domReadyTime = t.domComplete - t.domInteractive;
    // 白屏时间耗时
    obj.block_time = t.responseStart - t.navigationStart;
    // DOMready时间
    obj.dom_ready = t.domContentLoadedEventEnd - t.navigationStart;
    // 页面加载完成的时间 即：onload时间
    obj.load_time = t.loadEventEnd - t.navigationStart;

    let _obj=obj;
    if(type||false){
      _obj=obj[type];
    }

    return _obj;
  }


  //组装控制台输出信息
  creatLogString(level, log) {
    let str = this.getDateString();
    str += '[--' + level + '--]';
    if (typeof log == 'string') {
      str += log;
    } else if (typeof log == 'object' && log.hasOwnProperty('info')) {
     
      str += '@['+log.module+"]";
      str += log.info;
    } else {
      str += '日志信息错误,请检查!';
    }

    return str;
  }

  // 仅在控制台输出的日志
  trace(level, log) {
    let logStr = this.creatLogString(level, log);
    let _log = '';
    if (typeof log == 'object') {
      _log = log;
    }

    logger[level].call(this, logStr, _log);
  }

  // 需要保存的日志
  log(level, info) {
    logger[level].call(this, info);
  }

  /**
   * 获取当前浏览器信息
   * @param {} module 
   */

   getExpInfo(){

   }


   /**
    * react单元渲染时间
    * 
    */
   getReactDrawTime(module='未知模块'){
    let nowTime=new Date().getTime();
    let value=nowTime-this.mountTime;
    if(value>1000000000){
      return;
    }
    const logboj={
      info:'渲染当前页面时长为'+value+'ms',
      module:module,
    }

    let level='info';
    if(value>2000){
      level='warn'
    }

    let logStr = this.creatLogString(level, logboj);
    //console.log(logStr,logger,logger[level]);
    logger[level].call(this, logStr);

   }


  /**
   * 获取白屏时间
   * 打开网站到有内容渲染出来的时间点
   */
  getBlankTime(module='未知模块') {
    let value=this.getPerformanceTiming(timeType.BLOCK_TIME);
    const logboj={
      info:'当前页面白屏时间为'+value+'ms',
      module:module,
    }

    let level='info';
    if(value>2000){
      level='warn'
    }

    let logStr = this.creatLogString(level, logboj);
    //console.log(logStr,logger,logger[level]);
    logger[level].call(this, logStr);
  }

  /**
   * 获取首屏时间
   * 首屏内容渲染完毕的时间节点
   */
  getFirstScreenTime() {
    let log=this.getPerformanceTiming();

    

    logger.info.call(this, log);
  }

  /**
   * 用户可操作时间节点
   * domready触发节点
   */
  getDomreadyTime(module='未知模块') {
    let value=this.getPerformanceTiming(timeType.DOM_READY);
    const logboj={
      info:'用户可操作时间为'+value+'ms',
      module:module,
    }

    let level='info';
    if(value>2000){
      level='warn'
    }

    let logStr = this.creatLogString(level, logboj);
    //console.log(logStr,logger,logger[level]);
    logger[level].call(this, logStr);
  }

  /**
   * 总下载时间
   * window.onload的触发节点
   */
  getOnloadTime(module='未知模块') {
    let value=this.getPerformanceTiming(timeType.LOAD_TIME);
    const logboj={
      info:'当前页面总下载时间为'+value+'ms',
      module:module,
    }

    let level='info';
    if(value>2000){
      level='warn'
    }

    let logStr = this.creatLogString(level, logboj);
    //console.log(logStr,logger,logger[level]);
    logger[level].call(this, logStr);
  }
}

export default new Rlog();
